using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace VotingSystem.Models
{
    public class Vote
    {
        [Key]
        public int Id { get; set; }

        [Required]
        [StringLength(100)]
        public string Title { get; set; } = string.Empty;

        [StringLength(1000)]
        public string? Description { get; set; }

        [Required]
        public DateTime CreateTime { get; set; }

        [Required]
        public DateTime EndTime { get; set; }

        public bool IsPublic { get; set; }

        public bool IsAnonymous { get; set; }

        public bool RequireLogin { get; set; }

        public VoteType Type { get; set; }

        public int MaxVotesPerUser { get; set; }

        public ResultVisibility ResultVisibility { get; set; }

        [StringLength(50)]
        public string? Password { get; set; }

        public bool AllowModify { get; set; }

        public bool ShowResultsAfterEnd { get; set; }

        [Required]
        public string CreatorId { get; set; } = string.Empty;

        [ForeignKey("CreatorId")]
        public ApplicationUser? Creator { get; set; }

        public virtual ICollection<VoteOption> Options { get; set; } = new List<VoteOption>();

        public virtual ICollection<VoteRecord> Records { get; set; } = new List<VoteRecord>();

        public bool IsEnabled { get; set; } = true;

        public int ViewCount { get; set; }

        [StringLength(50)]
        public string? Category { get; set; }

        [StringLength(500)]
        public string? Tags { get; set; }

        [StringLength(32)]
        public string? AccessToken { get; set; }

        public bool IsEnded => DateTime.Now >= EndTime;

        public bool IsActive => !IsEnded && IsEnabled;

        public int TotalVotes => Options.Sum(o => o.Votes);

        public bool CanVote(string? userId)
        {
            if (IsEnded) return false;
            if (RequireLogin && string.IsNullOrEmpty(userId)) return false;
            if (string.IsNullOrEmpty(userId)) return true;
            return AllowModify || !Records.Any(r => r.UserId == userId);
        }

        public bool CanViewResults(string? userId)
        {
            if (ResultVisibility == ResultVisibility.Always) return true;
            if (ResultVisibility == ResultVisibility.CreatorOnly) return userId == CreatorId;
            if (ResultVisibility == ResultVisibility.AfterEnd) return IsEnded;
            if (ResultVisibility == ResultVisibility.AfterVote) return Records.Any(r => r.UserId == userId);
            return false;
        }
    }

    public enum VoteType
    {
        [Display(Name = "单选")]
        Single,
        [Display(Name = "多选")]
        Multiple,
        [Display(Name = "评分")]
        Rating
    }

    public enum ResultVisibility
    {
        [Display(Name = "始终可见")]
        Always,
        [Display(Name = "投票后可见")]
        AfterVote,
        [Display(Name = "投票结束后可见")]
        AfterEnd,
        [Display(Name = "仅发起人可见")]
        CreatorOnly
    }
} 